package com.java.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.java.entity.RaceAccount;
import com.java.entity.RaceAccount;
import com.java.entity.event.RaceAccountEvent;
import com.java.mapper.RaceAccountMapper;
import com.java.service.AccountService;
import com.java.util.MyCons;
import com.java.util.MyMap;
import com.java.util.MyTools;
import com.java.util.resp.BaseRespone;
import com.java.util.resp.TableRespone;
import com.java.util.servlet.ServletUtil;

@Service
public class AccountServiceImpl implements AccountService {
	
	@Autowired private RaceAccountMapper accountMapper;

	@Override
	public BaseRespone saveAuth(String number, String openId, String nick, String logo) {
		//RaceAccount wx = accountMapper.selectByInfo(new RaceAccount().setAccountOpenId(openId));
		RaceAccount wx = accountMapper.selectByInfo(new RaceAccount().setAccountNumber(number));
		if(null == wx) {
			return BaseRespone.error("该工号/学号不存在");
		}
		if(null != wx && !openId.equals(wx.getAccountOpenId()) && null != wx.getAccountOpenId()) {
			return BaseRespone.error("该账号已被其他微信绑定");
		}
		if(wx.getAccountType() == 2) {
			return BaseRespone.error("非学生/教师账号无法绑定");
		}
		//wx = new RaceAccount();
		//wx.setAccountId(MyTools.getUUID())
		//wx.setAccountUser(MyTools.getStringRandom(6))
		wx.setAccountNumber(number)
		//.setAccountPassword("123456")
		//.setAccountName(MyTools.getStringRandom(6))
		.setAccountOpenId(openId)
		.setAccountNick(nick)
		.setAccountLogo(logo)
		.setAccountStatus(1)
		//.setAccountType(type)
		.setAccountTime(new Date());
		accountMapper.updateSelective(wx);
		return new BaseRespone(0, "登录成功", wx);
	}

	@Override
	public RaceAccount getAccount(String accountId) {
		return accountMapper.selectByKey(accountId);
	}

	@Override
	public RaceAccount getAccountByOpenId(String openId) {
		RaceAccount wx = accountMapper.selectByInfo(new RaceAccount().setAccountOpenId(openId));
		return wx;
	}

	@Override
	public BaseRespone login(Integer type, String number, String password) {
		RaceAccount info = accountMapper.selectByInfo(new RaceAccount().setAccountNumber(number));
		if(null == info) {
			return BaseRespone.error("该账号不存在");
		}
		if(info.getAccountStatus() == 0) {
			return BaseRespone.error("该账号未生效");
		}
		if(info.getAccountStatus() == 2) {
			return BaseRespone.error("该账号不可登录");
		}
		if(!info.getAccountPassword().equals(password)) {
			return BaseRespone.error("密码错误");
		}
		ServletUtil.getSession().setAttribute(MyCons.USER_ROLE, info);
		ServletUtil.getSession().setAttribute(MyCons.USER_ROLE_TYPE, info.getAccountType());
		return BaseRespone.success("登陆成功");
	}

	@Override
	public BaseRespone register(Integer type, String number, String password, String name) {
		RaceAccount info = accountMapper.selectByInfo(new RaceAccount().setAccountNumber(number));
		if(null != info) {
			return BaseRespone.error("该账号已被注册");
		}
		info = new RaceAccount();
		info.setAccountId(MyTools.getUUID())
		.setAccountType(type)
		.setAccountNumber(number)
		.setAccountPassword(password)
		.setAccountName(name)
		.setAccountTime(new Date())
		.setAccountStatus(1);
		int row = accountMapper.insertSelective(info);
		if(row == 0) {
			return BaseRespone.error("注册失败");
		}
		return BaseRespone.success("注册成功，请登录");
	}

	@Override
	public TableRespone getAccountList(Integer type, int page, int size) {
		MyMap mm = new MyMap().put("accountType", type);
		int offset = TableRespone.getOffset(page, size);
		List<RaceAccountEvent> list = accountMapper.selectAll(mm, offset, size);
		List<RaceAccountEvent> count = accountMapper.selectAll(mm, 0, 0);
		return new TableRespone(list, count.size());
	}

	@Override
	public BaseRespone update(RaceAccount account) {
		RaceAccount info = accountMapper.selectByKey(account.getAccountId());
		info.setAccountName(account.getAccountName())
		.setAccountNick(account.getAccountNick());
		if(!MyTools.isEmpty(account.getAccountLogo())) {
			info.setAccountLogo(account.getAccountLogo());
		}
		if(!MyTools.isEmpty(account.getAccountPassword())) {
			info.setAccountPassword(account.getAccountPassword());
		}
		int row = accountMapper.updateSelective(info);
		if(row > 0) {
			return BaseRespone.success("设置成功");
		}
		return BaseRespone.error("设置失败");
	}

	@Override
	public BaseRespone updateAccount(RaceAccount account) {
		RaceAccount info = accountMapper.selectByKey(account.getAccountId());
		RaceAccount numHas = accountMapper.selectByInfo(new RaceAccount()
				.setAccountNumber(account.getAccountNumber()));
		if(null != numHas && !numHas.getAccountId().equals(account.getAccountId())) {
			return BaseRespone.error("该"+(account.getAccountType()==0?"学号":"工号")+"已被使用");
		}
		info.setAccountNumber(account.getAccountNumber());
		info.setAccountName(account.getAccountName());
		info.setAccountNick(account.getAccountNick());
		if(!MyTools.isEmpty(account.getAccountPassword())) {
			info.setAccountPassword(account.getAccountPassword());
		}
		int row = accountMapper.updateSelective(info);
		return BaseRespone.result(row, "修改成功");
	}

	@Override
	public BaseRespone createAccount(RaceAccount account) {
		RaceAccount numHas = accountMapper.selectByInfo(new RaceAccount()
				.setAccountNumber(account.getAccountNumber()));
		if(null != numHas) {
			return BaseRespone.error("该"+(account.getAccountType()==0?"学号":"工号")+"已被使用");
		}
		account.setAccountId(MyTools.getUUID())
		.setAccountType(account.getAccountType())
		.setAccountNumber(account.getAccountNumber())
		.setAccountPassword(account.getAccountPassword())
		.setAccountName(account.getAccountName())
		.setAccountNick(account.getAccountNick())
		.setAccountTime(new Date())
		.setAccountStatus(1);
		int row = accountMapper.insertSelective(account);
		return BaseRespone.result(row, (account.getAccountType()==0?"注册成功":"创建成功"));
	}

}
